<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<ad unit-id="adunit-2f2458e7ddbddc18" ad-type="video" ad-theme="white"></ad>
		<!-- #endif -->
		<view class="uni-padding-wrap uni-common-mt">
			<view class="uni-title" style="margin-left: 18px;">倒计时：(请勿息屏)</view>
			<view>
				<slider value="10" max="30" min="1" @change="sliderChange" show-value />
			</view>
			<button type="primary" :disabled="tap" @click="start" style="-webkit-flex: 1;flex: 1; margin-top: 10px;">确定</button>
			<view style="text-align: center;font-size: 28px; color: red;">
				{{second}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				second: 0,
				sliderTime: 10,
				id: 0,
				tap:false,
				audio_list: getApp().globalData.audio_list
			}
		},
		onLoad(options) {
			console.log('options')
			console.log(options)
			this.id = options.id
			// 为了防止苹果手机静音无法播放
			uni.setInnerAudioOption({
				obeyMuteSwitch: false
			})
		},
		methods: {
			sliderChange(e) {
				this.sliderTime = e.detail.value
			},
			start() {
				console.log(this.sliderTime)
				this.second = this.sliderTime;
				this.tap = true;
				var set = setInterval(() => {
					this.second -= 1
					if (this.second === 0) {
						this.tap = false;
						this.playing(this.audio_list[this.id].audio_src)
						clearInterval(set);
					}
				}, 1000)
			},
			playing(src){
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = src;
				innerAudioContext.onPlay(() => {
					console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
					console.log(res.errMsg);
					console.log(res.errCode);
				});
			},
		}
	}
</script>

<style>

</style>
